/**
 * Created by YJH on 2018/5/30.
 */

import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    TouchableOpacity,
    Image,
    Button,
    AsyncStorage,
    TextInput,
} from 'react-native';
import keys from '../../../res/data/keys.json';
import langs from '../../../res/data/langs.json';

export var FLAG_LAN = {flagLanguage: 'flag_language_language', flagKey: 'flag_language_key'};
export default class LanguageDao {

    constructor(flag) {
        this.flag = flag;
    }

    fetch() {
        return new Promise((resolve, reject) => {
            AsyncStorage.getItem(this.flag, (error, result) => {
                if (error) {
                    reject(error);
                    return;
                }

                if (result) {
                    try {
                        resolve(JSON.parse(result));
                    } catch (error) {
                        reject(error);
                    }
                } else {
                    //如果是自定义标签
                    var data = this.flag === FLAG_LAN.flagKey ? keys : langs;
                    this.save(data);
                    resolve(data);
                }
            });
        });
    }

    save(data) {
        AsyncStorage.setItem(this.flag, JSON.stringify(data), (error) => {

        });
    }
}